package com.app.micaihu.view.user.usertask;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.GetTaskBean;
import com.app.micaihu.bean.task.RewardConscription;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

/**
 * Created by Guo on 2017/6/14.
 *
 * 奖励的View
 *
 */

public class RewardView extends LinearLayout implements View.OnClickListener {
//    0不可领取 1可领取 2已领取
    public static final String unreward = "0";
    public static final String rewarding = "1";
    public static final String rewarded = "2";

    private ImageView icon;
    private TextView num;
    private TextView reward1;
    private TextView reward2;
    private TextView btn;
    private RewardConscription rewardConscription;


    private Drawable rewared1Icon1 = AppApplication.getInstance().getResources().getDrawable(R.drawable.task_icmilitaryexploit_gray);
    private Drawable rewared1Icon2 = AppApplication.getInstance().getResources().getDrawable(R.drawable.task_icmilitaryexploit_red);
    private Drawable rewared2Icon1 = AppApplication.getInstance().getResources().getDrawable(R.drawable.task_icgold_gray);
    private Drawable rewared2Icon2 = AppApplication.getInstance().getResources().getDrawable(R.drawable.task_icgold_red);

    public RewardView(Context context, RewardConscription rewardConscription) {
        super(context);
        this.rewardConscription = rewardConscription;
        rewared1Icon1.setBounds(0, 0, rewared1Icon1.getMinimumWidth(), rewared1Icon1.getMinimumHeight());
        rewared1Icon2.setBounds(0, 0, rewared1Icon2.getMinimumWidth(), rewared1Icon2.getMinimumHeight());
        rewared2Icon1.setBounds(0, 0, rewared2Icon1.getMinimumWidth(), rewared2Icon1.getMinimumHeight());
        rewared2Icon2.setBounds(0, 0, rewared2Icon2.getMinimumWidth(), rewared2Icon2.getMinimumHeight());

        init();



    }

    public RewardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RewardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    private void init() {

        inflate(getContext(), R.layout.view_reward,this);
        icon = (ImageView) findViewById(R.id.conscriptionIcon);
        num = (TextView) findViewById(R.id.num);
        reward1 = (TextView) findViewById(R.id.reward1);
        reward2 = (TextView) findViewById(R.id.reward2);
        btn = (TextView) findViewById(R.id.conscriptionBtn);

//        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)getLayoutParams();
//        if(lp == null){
//            lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        }
//        lp.width = 0;
//        lp.weight = 1;
//        setLayoutParams(lp);
        if(rewardConscription != null){
            num.setText(rewardConscription.getCondition());
            reward1.setText(rewardConscription.getRewardScore());
            reward2.setText(rewardConscription.getRewardGold());
            refreshStatus(rewardConscription.getRewardStatus());



        }

    }


    /**
     * 刷新状态
     * @param type
     */
    public void refreshStatus(String type){
        //    0不可领取 1可领取 2已领取
        rewardConscription.setRewardStatus(type);
        switch (type){
            case unreward:
                icon.setImageResource(R.drawable.task_zhengbing_gray);
                reward1.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.usertask_unprice));
                reward2.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.usertask_unprice));

                reward1.setCompoundDrawables(null,null,rewared1Icon1,null);
                reward2.setCompoundDrawables(null,null,rewared2Icon1,null);

                btn.setText(AppApplication.getInstance().getString(R.string.task_unreceive));
                btn.setBackgroundResource(R.drawable.taskprice_unreceive_btn_bg);
                btn.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.usertask_unprice));
                btn.setClickable(false);
                break;
            case rewarding:
                icon.setImageResource(R.drawable.task_zhengbing_red);
                reward1.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.usertask_price));
                reward2.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.usertask_price));

                reward1.setCompoundDrawables(null,null,rewared1Icon2,null);
                reward2.setCompoundDrawables(null,null,rewared2Icon2,null);

                btn.setText(AppApplication.getInstance().getString(R.string.task_unreceive));
                btn.setBackgroundResource(R.drawable.taskprice_conscriptionreceive_btn_bg);
                btn.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_font_color_6));

                btn.setClickable(true);
                btn.setOnClickListener(this);
                break;
            case rewarded:
                icon.setImageResource(R.drawable.task_zhengbing_red);

                reward1.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.usertask_price));
                reward2.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.usertask_price));

                reward1.setCompoundDrawables(null,null,rewared1Icon2,null);
                reward2.setCompoundDrawables(null,null,rewared2Icon2,null);


                btn.setText(AppApplication.getInstance().getString(R.string.task_receive));

                btn.setBackgroundResource(R.drawable.taskprice_conscriptionreceived_btn_bg);
                btn.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_font_color_60));
                btn.setClickable(false);

                break;

        }
    }




    public void setRewardClickListener(OnClickListener onClickListener){

        icon.setOnClickListener(onClickListener);

    }


    public RewardConscription getRewardConscription() {
        return rewardConscription;
    }

    public void setRewardConscription(RewardConscription rewardConscription) {
        this.rewardConscription = rewardConscription;
    }

    @Override
    public void onClick(View v) {
        if(rewardConscription == null || !rewarding.equals(rewardConscription.getRewardStatus()) ) return;
        boolean login = UserInforManager.getInstance().isLogin();
        if(!login){
            UserInforManager.getInstance().toLoginPage((Activity) getContext());
        }else{
            ParamMap params = new ParamMap();
            params.putEncode("uid",UserInforManager.getInstance().getUserInfor().getUid());
            params.putEncode("type",6+"");
            params.putEncode("inviteNum",rewardConscription.getInvateNum()+"");
            HttpUtils.ParmsMapMD5(params);
            HttpListener<DataBean<GetTaskBean>> getRewardListener = new HttpListener<DataBean<GetTaskBean>>() {
                @Override
                public void onStart() {
                    super.onStart();
                    DialogUtils.getInstance().showProgressDialog((Activity) getContext(), getResources().getString(R.string.task_getreward));
                }

                @Override
                public void onError(VolleyError error) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    ToastUtils.makeText(getResources().getString(R.string.neterror));

                }

                @Override
                public void onSuccess(DataBean<GetTaskBean> response) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    if(!response.noError()){
                        if(TextUtils.equals(response.getCode(),"127")){
                            ToastUtils.makeText(getResources().getString(R.string.task_alreadyreceive));
                        }else{
                            ToastUtils.makeText(response.getMsg());
                        }
                    }else{
                        if(response.getData().getIsGetTask("征兵")){
                            UserInforManager.getInstance().getUserTask(getContext(),false);
                               refreshStatus(rewarded);
                        }
                    }
                }
            };
//            Activity context = (Activity) getContext();
//            String simpleName = context.getClass().getSimpleName();
//            LogUtils.e("gzq", "simpleName");
            HttpUtils.addPostRequest(URIContant.GETREWARD,new TypeToken<DataBean<GetTaskBean>>(){}.getType(),"",params,getRewardListener);
        }
    }



}
